import numpy as np
from seamData import randomSeam2
from genFragment import gen2
import datetime
import pandas as pd

if __name__ == "__main__":
    import json
    import pandas as pd
    import matplotlib.pyplot as plt
    np.random.seed(datetime.datetime.now().microsecond)
    with open("config.json",'r',encoding='utf8') as fi:
        config=json.load(fi)
        fi.close()
    fieldInConfig=[field for field in config["thresh"]]
    filePath = '../data/一号泵.csv'
    data = pd.read_csv(filePath, encoding="utf-8")
    ls=['B相电流', '有功功率', 'B相电压', '运行状态', '日耗电量', '定子温度', 'A相电压', 'A相电流', '定子前部温度', '泵后轴温度', '后轴承温度', 'C相电流', '定子中部温度', '润滑油压力', '泵前轴温度', '电机冷却水压力', '定子后部温度', '进口压力', '功率因数', '出口压力', '视在功率', '瞬时流量', '无功功率', 'C相电压', '前轴承温度', '周波']
    fields=list(set(ls).intersection(set(fieldInConfig)))
    fields.sort()  # 在使用之前排序
    thresh = [config["thresh"][field] for field in fields]
    data = data[fields]#data[PRESET_FIELDS]
    data = data.values
    seamData = randomSeam2(data, 15, [300, 400],thresh,waveColIdx=0)
    runStatus=np.ones((seamData.shape[0]))
    curTime=datetime.datetime(2020,4,9,9,10,0)
    format='%Y-%m-%d %H:%M:%S'
    times=[]
    for i in range(seamData.shape[0]):
        times.append(curTime.strftime(format))
        curTime=curTime+datetime.timedelta(minutes=1)
    times=np.array(times)
    missingField=list(set(fieldInConfig)-set(fields))
    missingField.sort()  # 在使用之前排序
    for field in missingField:
        simData=gen2(config["thresh"][field]['error'],[80,200],seamData.shape[0])
        seamData = np.concatenate([seamData, simData.reshape((simData.shape[0],1))], axis=1)
    fields.extend(missingField)
    # df = pd.DataFrame(seamData, columns=fields)
    # df.plot()
    # import matplotlib.pyplot as plt
    # plt.show()
    seamData = np.concatenate([seamData, runStatus.reshape((runStatus.shape[0], 1))], axis=1)
    seamData = np.concatenate([times.reshape((times.shape[0],1)),seamData], axis=1)
    fields.insert(0,"采集时间")
    fields.append("运行状态")
    df=pd.DataFrame(seamData,columns=fields)
    df.to_csv('./4.csv',index=False,header=True)